/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhaoshujin
 * Date: 2011-6-3 下午12:27:28
 */
package com.hash;

/**
 * @author zhaoshujin
 *
 */
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.Socket;

import org.apache.commons.net.telnet.TelnetClient;

public class TelnetTest
{
    private TelnetClient telnet = new TelnetClient();
   
    private InputStream in;
   
    private PrintStream out;
   
    private char prompt = '$';
    
    public TelnetTest(){}
   
    public TelnetTest(String server, int port ,String user, String password)
    {
        try
        {
            // Connect to the specified server
        	//超时设置不好用
        	telnet.setDefaultTimeout(5);
        	telnet.setDefaultPort(11211);
            telnet.connect(server);
           
            // Get input and output stream references
            in = telnet.getInputStream();
            //out = new PrintStream(telnet.getOutputStream());
            // Log the user on
//            readUntil("login: ");
//            write(user);
//            readUntil("Password: ");
//            write(password);
//            // Advance to a prompt
//            readUntil(prompt + " ");
            
            in.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
    public void su(String password)
    {
        try
        {
            write("su");
            readUntil("Password: ");
            write(password);
            prompt = '#';
            readUntil(prompt + " ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
    public String readUntil(String pattern)
    {
        try
        {
            char lastChar = pattern.charAt(pattern.length() - 1);
            StringBuffer sb = new StringBuffer();
            boolean found = false;
            char ch = (char)in.read();
            while (true)
            {
                System.out.print(ch);
                sb.append(ch);
                if (ch == lastChar)
                {
                    if (sb.toString().endsWith(pattern))
                    {
                        return sb.toString();
                    }
                }
                ch = (char)in.read();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
   
    public void write(String value)
    {
        try
        {
            out.println(value);
            out.flush();
            System.out.println(value);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
    public String sendCommand(String command)
    {
        try
        {
            write(command);
            return readUntil(prompt + " ");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
   
    public void disconnect()
    {
        try
        {
            telnet.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * 使用socket 功能，测试是否能够连接到服务器
     * @param ip
     * @param port
     */
    public ObjectOutputStream isLinkedServer(String ip , int port)
    {
    	ObjectOutputStream out = null;
		try {
			Socket s = new Socket(ip, port);
			
			out = new ObjectOutputStream(s.getOutputStream());
			//out.writeUTF("hello world ");
			
			out.flush();
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return out;
    }
   
    public static void main(String[] args)
    {
//        TelnetTest tt = new TelnetTest();
//        tt.isLinkedServer("10.0.2.211", 11211);
    	
    	TelnetTest tt1 = new TelnetTest("10.0.2.212", 11211, "", "");
    }
}

